package _2022.day._014_找出有序数组中绝对值最小的数;

/**
 * @author： YHSimon
 * @date： 2022-08-01 19:01
 */
public class Solution {
    public int getMinAbs(int[] nums) {
        int l = 0, h = nums.length - 1;
        if (nums.length == 1) {
            return nums[0];
        }

        if (nums[0] >= 0) {
            return nums[0];
        } else if (nums[h] <= 0) {
            return nums[h];
        }
        int m = (l + h) / 2;
        while (l < h) {
            if (nums[m] < 0) {
                l = m + 1;
            } else if (nums[m] > 0) {
                if (nums[m] * nums[m - 1] < 0) {
                    return Math.min(-nums[m - 1], nums[m]);
                }
                h = m - 1;
            } else {
                return nums[m];
            }
        }
        return 0;
    }

    public static void main(String[] args) {
        System.out.println(new Solution().getMinAbs(new int[]{-2, -1, 3, 3, 4, 5}));

    }
}
